using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using Jonathan.Classes;

namespace Jonathan
{
    public class GradientPanel : Panel
    {
        private Color m_Color1;
        private Color m_Color2;
        private LinearGradientMode m_GradientMode;
        private Border3DStyle m_BorderStyle;
        private bool m_Draw3DBorder;
        private StringTrimming m_Trimming;

        public GradientPanel()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);

            this.m_Color1 = SystemColors.Menu;
            this.m_Color2 = SystemColors.MenuBar;
            this.m_GradientMode = LinearGradientMode.Vertical;
            this.m_BorderStyle = Border3DStyle.Bump;
            this.m_Draw3DBorder = false;
            this.m_Trimming = StringTrimming.EllipsisCharacter;
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (this.Width > 0 & this.Height > 0)
            {
                //base.OnPaintBackground(e);
                LinearGradientBrush lBr = new LinearGradientBrush(this.ClientRectangle, this.m_Color1, this.m_Color2, this.m_GradientMode);
                e.Graphics.FillRectangle(lBr, this.ClientRectangle);
                lBr.Dispose();

                if (!string.IsNullOrEmpty(this.Text))
                {
                    int height = Convert.ToInt32(e.Graphics.MeasureString("ty", SystemFonts.SmallCaptionFont).Height);
                    StringFormat sf = Common.StringFormatAlignment(ContentAlignment.MiddleLeft);
                    sf.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.NoClip;
                    sf.Trimming = this.m_Trimming;
                    int offset = (this.Controls.Count > 0) ? 30 : 0;
                    e.Graphics.DrawString(this.Text, SystemFonts.SmallCaptionFont, Brushes.Black,
                        new RectangleF(5, 0, this.Width - offset, this.Height),
                        sf);
                }
                if(this.m_Draw3DBorder)
                    ControlPaint.DrawBorder3D(e.Graphics, this.ClientRectangle, this.m_BorderStyle);
            }
        }

        /// <summary>
        /// Gets or Sets the Trimming of the Text...
        /// </summary>
        public StringTrimming Trimming
        {
            get { return this.m_Trimming; }
            set { this.m_Trimming = value; }
        }

        /// <summary>
        /// Gets or Sets the First Color...
        /// </summary>
        public Color Color1
        {
            get { return this.m_Color1; }

            set
            {
                this.m_Color1 = value;
                this.Invalidate(true);
            }
        }

        /// <summary>
        /// Gets or Sets the Second Color...
        /// </summary>
        public Color Color2
        {
            get { return this.m_Color2; }

            set
            {
                this.m_Color2 = value;
                this.Invalidate(true);
            }
        }

        /// <summary>
        /// Gets or Sets the Gradient Mode...
        /// </summary>
        public LinearGradientMode GradientMode
        {
            get { return this.m_GradientMode; }

            set
            {
                this.m_GradientMode = value;
                this.Invalidate(true);
            }
        }

        /// <summary>
        /// Gets or Sets the Border Style...
        /// </summary>
        public Border3DStyle Border3DStyle
        {
            get { return this.m_BorderStyle; }
            set
            {
                this.m_BorderStyle = value;
                this.Invalidate(true);
            }
        }

        /// <summary>
        /// Gets or Sets if we should draw the 3D Border Style...
        /// </summary>
        public bool Draw3dBorder
        {
            get { return this.m_Draw3DBorder; }
            set
            {
                this.m_Draw3DBorder = value;
                this.Invalidate(true);
            }
        }
    }
}
